package com.yzyz.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import com.yzyz.bean.scenic.AttractionsBean;
import com.yzyz.dao.AttractionsDao;
import com.yzyz.utils.mapper.AttractionsRowMapper;

/**
 * 
 * @author David Sun
 * @Date   2010-4-5
 *
 */
public class AttractionsDaoImpl implements AttractionsDao {

	private JdbcTemplate jdbcTemplate;
	@SuppressWarnings("unchecked")
	@Override
	public List<AttractionsBean> getAll(int ownerId) {
		String sql = "select * from attractions where ownerId=" + ownerId;
		return this.jdbcTemplate.queryForList(sql);
	}

	@Override
	public int delete(int id) {
		String sql = "delete from attractions where id=" + id;
		return this.jdbcTemplate.update(sql);

	}

	@SuppressWarnings("unchecked")
	@Override
	public Object get(int id) {
		String sql = "select * from attractions where id=" + id;
		List<AttractionsBean> list = this.jdbcTemplate.query(sql, new AttractionsRowMapper());
		return  list == null?null:list.get(0);
	}

	@Override
	public int save(Object object) {
		final String sql = "insert into attractions (name,description,date,ownerId) values(?,?,?,?)";
		final AttractionsBean d = (AttractionsBean)object;
		KeyHolder keyHolder = new GeneratedKeyHolder();
		this.jdbcTemplate.update(new PreparedStatementCreator() {
			public java.sql.PreparedStatement createPreparedStatement(
					Connection conn) throws SQLException {
				java.sql.PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
				ps.setString(1, d.getName());
				ps.setString(2, d.getDescription());
				ps.setTimestamp(3, new java.sql.Timestamp(d.getDate().getTime()));
				ps.setInt(4, d.getOwnerId());
				return ps;
			}
		}, keyHolder);
		return keyHolder.getKey().intValue();
	}

	@Override
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public void update(Object object) {
		final AttractionsBean att = (AttractionsBean)object;
		String sql = "update attractions set name=?, description=? where id=?";
		this.jdbcTemplate.update(sql, new PreparedStatementSetter(){

			@Override
			public void setValues(PreparedStatement ps) throws SQLException {
				ps.setString(1, att.getName());
				ps.setString(2, att.getDescription());
				ps.setInt(3, att.getId());
				
			}});

	}

	@Override
	public int updateAvailable(final int id, final boolean available) {
		String sql = "update attractions set available=? where id=?";
		return this.jdbcTemplate.update(sql, new PreparedStatementSetter(){

			@Override
			public void setValues(PreparedStatement ps) throws SQLException {
				ps.setBoolean(1, available);
				ps.setInt(2, id);
				
			}});
	}

	@Override
	public int getCount(int scenicId) {
		String sql = "select count(*) from attractions where ownerId=" + scenicId;
		return this.jdbcTemplate.queryForInt(sql);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AttractionsBean> paginate(int scenicId, int start, int limit) {
		String sql = "select * from attractions where ownerId=" + scenicId + " limit " + start + ", " + limit;
		return this.jdbcTemplate.query(sql, new AttractionsRowMapper());
	}

}
